package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.services.BaseService;
import com.dmcb.common.business.utils.DateUtil;
import com.dmcb.common.business.utils.RedisUtils;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.common.business.utils.WebClientUtil;
import com.dmcb.trade.business.entities.Article;
import com.dmcb.trade.business.mappers.ArticleCrawlMapper;
import com.dmcb.trade.controller.ArticleCrawlerController;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 知音网文章解析
 * Created by wangyong on 2017/8/1.
 */
@Service
public class ZhiYinCrawler extends BaseService {

    Logger logger = LoggerFactory.getLogger(ZhiYinCrawler.class);
    @Autowired
    private WebClientUtil webClientUtil;
    @Autowired
    private ArticleCrawlerController articleCrawlerController;
    @Autowired
    private ArticleCrawlMapper articleCrawlMapper;

    public JsonResult crawler() {
        WebClientUtil.wc = webClientUtil.initWc(false,false,true);
        int success = 0;
        int page = 1;
        String link = "http://www.zhiyin.cn/qinggan/qingganstory/";
        while (true) {
            Document doc = webClientUtil.getDoc(link);
            if (doc == null) {
                return error("解析主页失败");
            }
            Elements elements = doc.select("div.list_kj");
            if (elements == null || elements.size() == 0) {
                break;
            }
            int ero = 0;
            for (Element element : elements) {
                String src = element.select("div.list_youbt>a").attr("href");
                JsonResult jsonResult = crawlArticle(src);
                if (jsonResult.getStatus() != JsonResult.STATUS_SUCCESS) {
                    if (jsonResult.getStatus() == JsonResult.STATUS_ERROR) {
                        RedisUtils.changeIP(15);
                        logger.error("出错链接:"+src);
                        ero++;
                        if (ero >= 10) {
                            return error("连续出错10次，请核实原因");
                        }
                    }
                } else {
                    ero = 0;
                    success++;
                }
            }
            page++;
            link = link + page + ".html";
        }
        return success(success);
    }


    public JsonResult crawlArticle(String link) {
        boolean hasMore = false;
        int authorId = 2739;
        int page = 2;
        Document document = webClientUtil.getDoc(link);
        if (document == null) return error("解析失败");
        if (!document.select("span.huise").text().equals("知音网原创"))
            return warn(null, "非原创");
        Elements elements = document.select("div#pages>a");
        if (elements != null && elements.size() > 0) {
            page = elements.size() - 1;
            hasMore = true;
        }

        JsonResult jsonResult;
        if (hasMore) {
            jsonResult = crawlMoreArticle(authorId, link, document, page);
        } else {
            jsonResult = articleCrawlerController.crawl(authorId, link);

        }
        return jsonResult;

    }

    public JsonResult crawlMoreArticle(int authorId, String link, Document document, int page) {
        //http://www.zhiyin.cn/2017/meiwen_0515/580154.html
        //http://www.zhiyin.cn/2017/meiwen_0515/580154_2.html
        String src = link.substring(0, link.lastIndexOf(".html"));
        String title = document.select("div.article_nei h1").text();
        String auhthor = document.select("span.huise").text();
        String time = document.select("div.article_bt").text();
        String content = document.select("div.article_wz").text();
        String coverPath = document.select("div.article_wz>p>a>img").attr("src");
        for (int i = 2; i <= page; i++) {
            link = src + "_" + i + ".html";
            content += document.select("div.article_wz").text();
        }
        Article article = articleCrawlMapper.queryByAuthorTitle(authorId, title);
        if (article == null) {
            // 新文章
            article = new Article();
            article.setAuthorId(authorId);
            article.setTitle(title);
            article.setAuthorName(auhthor);
            article.setLink(link);
            article.setCoverPath(coverPath);
            article.setPostTime(DateUtil.parseDate(time));
        }
        // 如果已经存在，直接返回
        if (article.getId() > 0) {
            return warn("", "文章以存在");
        }
        article.setBody(content);
        articleCrawlMapper.insert(article);
        return success();


    }
}
